﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PlatForm_Ask : System.Web.UI.Page
{
	/// <summary>
	/// 单位控制类
	/// </summary>
	private PX.Department.DepartmentComponent clsDepartmentComp = new PX.Department.DepartmentComponent();
	/// <summary>
	/// 通用控件
	/// </summary>
	private PX.CommonControl clsCommonControl = new PX.CommonControl();
	/// <summary>
	/// 用户控制类
	/// </summary>
	private PX.User.UserComponent clsUserComp = new PX.User.UserComponent();
	/// <summary>
	/// 提问者控制类
	/// </summary>
	private PX.Asker.Component clsAskerComp = new PX.Asker.Component();
	/// <summary>
	/// 专家控制类
	/// </summary>
	/// <![CDATA[2014-7-16 17:28:40]]>
	private PX.Expert.ExpertComponent clsExpertComp = new PX.Expert.ExpertComponent();
	/// <summary>
	/// 问题控制类
	/// </summary>
	private PX.Question.Component clsQuestionComp = new PX.Question.Component();

	protected void Page_Load(object sender, EventArgs e)
	{
		int nUserID = 0;

		if (!IsPostBack)
		{
			if (Session["LoginUserID"] != null)
			{
				nUserID = Convert.ToInt32(Session["LoginUserID"]);

				ShowLoginInfo(nUserID);

			}
			else
			{
				ShowAnonymous();
			}

			ShowTitle();
		}
	}

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);

		clsDepartmentComp.page = Page;
		clsUserComp.page = Page;
		clsAskerComp.page = Page;
		clsQuestionComp.page = Page;

		clsCommonControl.Department(DdlDepartment);

		LBtnAddQuestion.Click += new EventHandler(LBtnAddQuestion_Click);
		
	}

	

	void LBtnAddQuestion_Click(object sender, EventArgs e)
	{
		PX.Question.Entity clsAddQuestion = new PX.Question.Entity();
		PX.Asker.Entity clsAsker = new PX.Asker.Entity();
		int nAskerUserID = 0;
		int nExpertUserID = 0;

		nAskerUserID = Convert.ToInt32(Session["LoginUserID"]);
		try
		{
			nExpertUserID = clsExpertComp.GetOne(Convert.ToInt32(Request["ExpertID"])).UserID;
		}
		catch
		{
			nExpertUserID = 0;
		}

		clsAsker = clsAskerComp.Query(nAskerUserID);
		if (clsAsker != null)
		{
			clsAsker.Name = TbName.Text;
			clsAsker.DepartmentID = Convert.ToInt32(DdlDepartment.Text);
			clsAsker.Email = TbEMail.Text;
			clsAsker.Phone = TbPhone.Text;

			if (clsAskerComp.Modify(clsAsker))
			{
				clsAddQuestion.AskerUserID = nAskerUserID;
				clsAddQuestion.ExpertUserID = nExpertUserID;
				clsAddQuestion.Content = TbQuestion.Text;
                
				if (clsQuestionComp.AddOffLine(clsAddQuestion))
				{
					Common.ShowMessageAlert.ShowMessage(Page, "提问成功！");
					TbQuestion.Text = "";
				}
			}
		}

		
	}

	/// <summary>
	/// 在控件显示登录用户信息
	/// </summary>
	private void ShowLoginInfo(int nUserID)
	{
		PX.Asker.Entity clsAsker = new PX.Asker.Entity();

		clsAsker = clsAskerComp.Query(nUserID);

		if (clsAsker != null)
		{
			TbName.Text = clsAsker.Name;
			try
			{
				DdlDepartment.Text = clsAsker.DepartmentID.ToString();
			}
			catch
			{

			}
			TbEMail.Text = clsAsker.Email;
			TbPhone.Text = clsAsker.Phone;
		}
		else
		{
			ShowAnonymous();
		}
	}

	/// <summary>
	/// 新添加一个匿名用户，并在控件显示信息
	/// </summary>
	/// <![CDATA[2014-7-16 17:47:36]]>
	private void ShowAnonymous()
	{
		PX.Asker.Entity clsAnonymousAsker = new PX.Asker.Entity();
		int nAnonymousUserID = 0;

		clsAskerComp.AddAnoynmous();
		nAnonymousUserID = clsAskerComp.GetNewestAddID();

		clsAnonymousAsker = clsAskerComp.GetOne(nAnonymousUserID);

		TbName.Text = clsAnonymousAsker.Name;

		Session["LoginUserID"] = clsAnonymousAsker.UserID;
	}

	/// <summary>
	/// 显示标题
	/// </summary>
	/// <![CDATA[2014-7-16 17:30:46]]>
	private void ShowTitle()
	{
		PX.Expert.Expert clsExpert = new PX.Expert.Expert();
		int nExpertID = 0;

		nExpertID = Convert.ToInt32(Request["ExpertID"]);

		clsExpert = clsExpertComp.GetOne(nExpertID);

		LabTitle.Text = "向" + clsExpert.Name + "提问";
	}
}
